package com.framework.modules.job.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.common.utils.DateUtils;
import com.framework.modules.app.entity.MixedRewardsToplistEntity;
import com.framework.modules.app.service.MixedRewardsToplistService;
import com.framework.modules.base.entity.StatLiveRewardsEntity;
import com.framework.modules.base.service.UserVipinfoService;
import com.framework.modules.live.service.LiveRewardsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@DependsOn("databaseInitConfig")
@Component("liveRewardsTopListUpdatedTask")
public class LiveRewardsTopListUpdatedTask {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private LiveRewardsService liveRewardsService;

    @Autowired
    private MixedRewardsToplistService mixedRewardsToplistService;

    public void run(String params) {
        logger.info("当前任务参数为：" + params);
        Boolean result = true;
        switch (params){
            case "hour":updateHourTopList();break;
            case "daily":updateDailyTopList();break;
            case "week":updateWeekTopList();break;
            case "month":updateMonthTopList();break;
            case "total":updateTotalTopList();break;
            default:;
        }
        logger.info(String.format("任务完成 liveRewardsTopListUpdatedTask.run 返回值： %s", result));
    }

    /**
     * 更新小时榜(全平台)
     */
    public void updateHourTopList(){
        logger.info("更新小时榜开始=====start");
        //1.先删除更新小时榜全部数据
        mixedRewardsToplistService.delete(new EntityWrapper<MixedRewardsToplistEntity>().eq("category","hour"));

        //2.查出本小时开始时间
        Date nowDate=new Date();
        Long nowTime=nowDate.getTime()/1000;
        Long beginOfHour=nowTime-3600;
        //3.统计本周数据
        List<StatLiveRewardsEntity> statLiveRewardsEntities=liveRewardsService.statLiveRewardsTopListToHour(beginOfHour.intValue(),nowTime.intValue());
        List<MixedRewardsToplistEntity> mixedRewardsToplistEntityList=new ArrayList<>();
        for(int i=0;i<statLiveRewardsEntities.size();i++){
            StatLiveRewardsEntity statLiveRewardsEntity=statLiveRewardsEntities.get(i);
            MixedRewardsToplistEntity mixedRewardsToplistEntity=new MixedRewardsToplistEntity();
            mixedRewardsToplistEntity.setAdddate(nowTime.intValue());
            mixedRewardsToplistEntity.setAvanta(statLiveRewardsEntity.getAvanta());
            mixedRewardsToplistEntity.setCategory("hour");
            mixedRewardsToplistEntity.setCoins(statLiveRewardsEntity.getTotalCoin());
            mixedRewardsToplistEntity.setLiveUid(statLiveRewardsEntity.getUid());
            mixedRewardsToplistEntity.setNickname(statLiveRewardsEntity.getNickname());
            mixedRewardsToplistEntity.setUid(statLiveRewardsEntity.getFromUid());
            mixedRewardsToplistEntity.setRank(i+1);
            mixedRewardsToplistEntityList.add(mixedRewardsToplistEntity);
        }
        if (mixedRewardsToplistEntityList.size()>0) mixedRewardsToplistService.insertBatch(mixedRewardsToplistEntityList);
        logger.info("更新小时榜完成======end");
    }

    /**
     * 更新日榜
     */
    public void updateDailyTopList(){
        logger.info("更新日榜开始=====start");
        //1.先删除更新日榜全部数据
        mixedRewardsToplistService.delete(new EntityWrapper<MixedRewardsToplistEntity>().eq("category","daily"));

        //2.查出日榜开始时间
        Date nowDate=new Date();
        Long beginOfHour=DateUtil.beginOfDay(nowDate).getTime()/1000;
        Long nowTime=nowDate.getTime()/1000;

        //3.统计本周数据
        List<StatLiveRewardsEntity> statLiveRewardsEntities=liveRewardsService.statLiveRewardsTopList(beginOfHour.intValue(),nowTime.intValue());
        List<MixedRewardsToplistEntity> mixedRewardsToplistEntityList=new ArrayList<>();
        for(int i=0;i<statLiveRewardsEntities.size();i++){
            StatLiveRewardsEntity statLiveRewardsEntity=statLiveRewardsEntities.get(i);
            MixedRewardsToplistEntity mixedRewardsToplistEntity=new MixedRewardsToplistEntity();
            mixedRewardsToplistEntity.setAdddate(nowTime.intValue());
            mixedRewardsToplistEntity.setAvanta(statLiveRewardsEntity.getAvanta());
            mixedRewardsToplistEntity.setCategory("daily");
            mixedRewardsToplistEntity.setCoins(statLiveRewardsEntity.getTotalCoin());
            mixedRewardsToplistEntity.setLiveUid(statLiveRewardsEntity.getUid());
            mixedRewardsToplistEntity.setNickname(statLiveRewardsEntity.getNickname());
            mixedRewardsToplistEntity.setUid(statLiveRewardsEntity.getFromUid());
            mixedRewardsToplistEntityList.add(mixedRewardsToplistEntity);
        }
        if (mixedRewardsToplistEntityList.size()>0) mixedRewardsToplistService.insertBatch(mixedRewardsToplistEntityList);
        logger.info("更新日榜完成======end");
    }


    /**
     * 更新周榜
     */
    public void updateWeekTopList(){
        logger.info("更新周榜开始=====start");
        //1.先删除本周榜单全部数据
        mixedRewardsToplistService.delete(new EntityWrapper<MixedRewardsToplistEntity>().eq("category","week"));

        //2.查出本周开始时间
        Date nowDate=new Date();
        Long beginOfWeek=DateUtil.beginOfWeek(nowDate).getTime()/1000;
        Long nowTime=nowDate.getTime()/1000;
        //3.统计本周数据
        List<StatLiveRewardsEntity> statLiveRewardsEntities=liveRewardsService.statLiveRewardsTopList(beginOfWeek.intValue(),nowTime.intValue());
        List<MixedRewardsToplistEntity> mixedRewardsToplistEntityList=new ArrayList<>();
        for(int i=0;i<statLiveRewardsEntities.size();i++){
            StatLiveRewardsEntity statLiveRewardsEntity=statLiveRewardsEntities.get(i);
            MixedRewardsToplistEntity mixedRewardsToplistEntity=new MixedRewardsToplistEntity();
            mixedRewardsToplistEntity.setAdddate(nowTime.intValue());
            mixedRewardsToplistEntity.setAvanta(statLiveRewardsEntity.getAvanta());
            mixedRewardsToplistEntity.setCategory("week");
            mixedRewardsToplistEntity.setCoins(statLiveRewardsEntity.getTotalCoin());
            mixedRewardsToplistEntity.setLiveUid(statLiveRewardsEntity.getUid());
            mixedRewardsToplistEntity.setNickname(statLiveRewardsEntity.getNickname());
            mixedRewardsToplistEntity.setUid(statLiveRewardsEntity.getFromUid());
            mixedRewardsToplistEntityList.add(mixedRewardsToplistEntity);
        }
        if (mixedRewardsToplistEntityList.size()>0) mixedRewardsToplistService.insertBatch(mixedRewardsToplistEntityList);
        logger.info("更新周榜完成======end");
    }

    /**
     * 更新月榜
     */
    public void updateMonthTopList(){
        logger.info("更新月榜开始=====start");
        //1.先删除本月榜单全部数据
        mixedRewardsToplistService.delete(new EntityWrapper<MixedRewardsToplistEntity>().eq("category","month"));

        //2.查出本月开始时间
        Date nowDate=new Date();
        Long beginOfMonth=DateUtil.beginOfMonth(nowDate).getTime()/1000;
        Long nowTime=nowDate.getTime()/1000;
        //3.统计本月数据
        List<StatLiveRewardsEntity> statLiveRewardsEntities=liveRewardsService.statLiveRewardsTopList(beginOfMonth.intValue(),nowTime.intValue());
        List<MixedRewardsToplistEntity> mixedRewardsToplistEntityList=new ArrayList<>();
        for(int i=0;i<statLiveRewardsEntities.size();i++){
            StatLiveRewardsEntity statLiveRewardsEntity=statLiveRewardsEntities.get(i);
            MixedRewardsToplistEntity mixedRewardsToplistEntity=new MixedRewardsToplistEntity();
            mixedRewardsToplistEntity.setAdddate(nowTime.intValue());
            mixedRewardsToplistEntity.setAvanta(statLiveRewardsEntity.getAvanta());
            mixedRewardsToplistEntity.setCategory("month");
            mixedRewardsToplistEntity.setCoins(statLiveRewardsEntity.getTotalCoin());
            mixedRewardsToplistEntity.setLiveUid(statLiveRewardsEntity.getUid());
            mixedRewardsToplistEntity.setNickname(statLiveRewardsEntity.getNickname());
            mixedRewardsToplistEntity.setUid(statLiveRewardsEntity.getFromUid());
            mixedRewardsToplistEntityList.add(mixedRewardsToplistEntity);
        }
        if (mixedRewardsToplistEntityList.size()>0) mixedRewardsToplistService.insertBatch(mixedRewardsToplistEntityList);
        logger.info("更新月榜完成======end");
    }

    /**
     * 更新总榜
     */
    public void updateTotalTopList(){
        logger.info("更新总榜开始=====start");
        //1.先删除总榜单全部数据
        mixedRewardsToplistService.delete(new EntityWrapper<MixedRewardsToplistEntity>().eq("category","total"));

        Date nowDate=new Date();
        //2.查出开始时间 默认开始时间2020/10/20
        Long beginOfYear=1603177851l;
        Long nowTime=nowDate.getTime()/1000;
        //3.统计本周数据
        List<StatLiveRewardsEntity> statLiveRewardsEntities=liveRewardsService.statLiveRewardsTopList(beginOfYear.intValue(),nowTime.intValue());
        List<MixedRewardsToplistEntity> mixedRewardsToplistEntityList=new ArrayList<>();
        for(int i=0;i<statLiveRewardsEntities.size();i++){
            StatLiveRewardsEntity statLiveRewardsEntity=statLiveRewardsEntities.get(i);
            MixedRewardsToplistEntity mixedRewardsToplistEntity=new MixedRewardsToplistEntity();
            mixedRewardsToplistEntity.setAdddate(nowTime.intValue());
            mixedRewardsToplistEntity.setAvanta(statLiveRewardsEntity.getAvanta());
            mixedRewardsToplistEntity.setCategory("total");
            mixedRewardsToplistEntity.setCoins(statLiveRewardsEntity.getTotalCoin());
            mixedRewardsToplistEntity.setLiveUid(statLiveRewardsEntity.getUid());
            mixedRewardsToplistEntity.setNickname(statLiveRewardsEntity.getNickname());
            mixedRewardsToplistEntity.setUid(statLiveRewardsEntity.getFromUid());
            mixedRewardsToplistEntityList.add(mixedRewardsToplistEntity);
        }
        if (mixedRewardsToplistEntityList.size()>0) mixedRewardsToplistService.insertBatch(mixedRewardsToplistEntityList);
        logger.info("更新总榜完成======end");
    }

    public void test2() {
        logger.info("我是不带参数的test2方法，正在被执行");
    }
}
